<template>
  <!-- 信用量管理 -->
  <div>
    <section class="business-wrapper">
      <el-form
        ref="form"
        :model="queryForm"
        :inline="true"
        class="queryForm adjustCondition headerCondition"
        :class="{'more': showMore, 'less': !showMore}"
        labelPosition="left"
      >
        <el-row class="more-content">
          <el-form-item label="业务类型:">
            <el-radio-group v-model="queryForm.modelId"  >
              <el-radio-button :label="item.value" :key="item.value" v-for="item in  processTypeData">{{ item.label }}
              </el-radio-button>
            </el-radio-group>
          </el-form-item>
        </el-row>
        <el-row class="more-content">
          <el-form-item label="业务日期:">
            <el-radio-group v-model="ckecked" @change="changeTime">
              <el-radio-button label="1">今日</el-radio-button>
              <el-radio-button label="2">本周</el-radio-button>
              <el-radio-button label="3">本月</el-radio-button>
              <el-radio-button label="4">上月</el-radio-button>
              <el-radio-button label="5">本年</el-radio-button>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="自定义:" class="customized">
            <TimeSelect times="times" @getTime="getTime1"></TimeSelect>
          </el-form-item>
        </el-row>
        <el-row class="business-status more-content">
          <el-form-item label="业务状态:">
            <el-radio-group v-model="queryForm.state" >
              <el-radio-button :label="item.value" :key="item.value"
                               v-for="item in  $dictUtils.getDictList('busState')">
                {{ item.label }}
              </el-radio-button>
            </el-radio-group>
          </el-form-item>
        </el-row>
        <el-row class="business-status search-content" v-if="!showMore" >
          <el-form-item label="关键字:">
            <el-input
              v-model="queryForm.keyWord"
              size="large"
              style="width: 400px;"
              placeholder="请输入客户名称/产品名称/合同编号查询"
            ></el-input>
          </el-form-item>
        </el-row>
        <el-row class="more-content getMoney-type">
          <el-form-item label="收款方式:">
            <el-radio-group v-model="queryForm.getMoneyType">
              <el-radio-button :label="item.value" :key="item.value"
                               v-for="item in  $dictUtils.getDictList('getMoneyType')">{{ item.label }}
              </el-radio-button>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="数据类别:">
            <el-radio-group v-model="queryForm.dataType">
              <el-radio-button :label="item.value" :key="item.value"
                               v-for="item in  $dictUtils.getDictList('dataType')">
                {{ item.label }}
              </el-radio-button>
            </el-radio-group>
          </el-form-item>
        </el-row>
        <el-row class="more-content company-name" style="margin-bottom:20px;">


          <el-form-item label="客户名称:" v-if="processType!=2">

            <company-name placeholder="请输入客户名称" @getCompanyData="getCompanyData"></company-name>
          </el-form-item>


          <el-form-item label="供应商名称:" v-else label-width="90px">
            <el-input class="selectInput" placeholder="请选择供应商名称" v-model="queryForm.companyName"
                      @focus="getCompanyClient">
              <!--            <template slot="append"><span @click="getCompanyClient">选择</span></template>-->
            </el-input>
          </el-form-item>
          <el-form-item label="产品名称:" v-if="processType!=2" >
            <product-name placeholder="请输入产品名称" @getProductData="getProductData"></product-name>
          </el-form-item>
          <el-form-item label="原料名称:" v-else>
            <el-input class="selectInput" placeholder="请选择原料名称" v-model="queryForm.productName"
                      @focus="getProductClient">
              <!--            <template slot="append"><span @click="getProductClient">选择</span></template>-->
            </el-input>
          </el-form-item>
        </el-row>
        <el-row class="btn-content">
          <!--     未生效 执行中     -->
          <el-form-item label="业务状态:"  v-if="typeFather==='2'||typeFather==='3'">
            <el-select clearable v-model="queryForm.state" placeholder="请选择"  style="width: 175px">
              <el-option
                v-for="item in $dictUtils.getDictList('busState')"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="业务类型:"  v-if="typeFather==='2'||typeFather==='3'" style="margin:0 50px;">
            <el-select clearable v-model="queryForm.modelId" placeholder="请选择" style="width: 175px">
              <el-option
                v-for="item in processTypeData"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <!--     未生效 执行中     -->
          <el-form-item>
            <div class="but-container">
              <el-button
                type="primary"
                size="small"
                style="margin-right:5px"
                class="search-btn"
                @click="
                getList(
                  processType,
                  page,
                  orderBy.name,
                  orderBy.method,
                  queryForm.dateTime,
                  queryForm.state,
                  queryForm.getMoneyType,
                  queryForm.companyName,
                  queryForm.productId,
                  queryForm.modelId,
                  queryForm.dataType,
                  queryForm.keyWord
                )">查询
              </el-button>
              <el-button size="small" @click="reset">重置</el-button>
              <!--            <el-button size="small" style="border: 1px solid rgb(230,162,60);color:rgb(230,162,60) ;"  @click="saveSearch" v-if="showMore">保存为常用查询条件</el-button>-->

              <!--    全部有筛选条件         -->
              <div class="more-button" @click="handleShowMore"
                   v-if="typeFather==='1'"
              >
                <span v-if="!this.showMore">更多</span>
                <span v-if="this.showMore">收起</span>筛选条件
                <i :class="{ 'el-icon-arrow-up' : this.showMore , 'el-icon-arrow-down': !this.showMore}"></i>
              </div>
              <!--    全部有筛选条件         -->
            </div>
          </el-form-item>
        </el-row>
      </el-form>
    </section>
    <div class="btn-wrapper">
      <el-button type="primary" @click="goToAddForm" size="small">新增</el-button>
      <section class="right-but adjustRight-but" style="display:flex">
        <el-button style="cursor:pointer;" type="text"
                   @click="exportExcel(processType,
                  page,
                  orderBy.name,
                  orderBy.method,
                  queryForm.dateTime,
                  queryForm.state,
                  queryForm.getMoneyType,
                  queryForm.companyName,
                  queryForm.productId,
                  queryForm.modelId,
                  queryForm.dataType,
                  queryForm.keyWord)">
          <img src="~@/assets/img/export.png" alt="">
        </el-button>
        <el-button type="text" style="margin-right:-13px;cursor:pointer;" @click="refreshList">
          <img style="width:44px;height:36px;" src="~@/assets/img/refresh.png">
        </el-button>
        <PopoverBut :checkedProps="checkedProps" :options="options" @getCheckeds="handlerCloumn"></PopoverBut>
      </section>
    </div>

    <!-- table 表格 -->

    <el-table
      size="mini"
      v-loading="loading"
      :data="tableData"
      style="width: 100%; margin-top: 10px"
      border
      :header-cell-style="{
        'background-color': '#eeeeee !important',
      }"
      :row-class-name="tableRowClassName"
      @selection-change="tableSelected"
    >
      <el-table-column
        type="selection"
        width="60">
      </el-table-column>
      <el-table-column type="index" label="序号" width="70" align="center">
        <template slot-scope="scope">
          <span>{{
              (page.pageNo - 1) * page.pageSize + scope.$index + 1
            }}</span>
        </template>
      </el-table-column>
      <el-table-column label="业务编号" width="230">
        <template slot-scope="scope">
          <el-link type="primary" :underline="false" style="padding-right: 10px"
                   @click="getSettingClient(scope.row,'业务详情')">{{ scope.row.codes }}
          </el-link>
        </template>
      </el-table-column>

      <el-table-column prop="modelName" label="业务类型" width="140"></el-table-column>
      <el-table-column prop="productNames" :label="productValue" width="150" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          {{ scope.row.productNames | noValue }}
        </template>
      </el-table-column>
      <el-table-column prop="companyName" :label="companyValue" width="150">
        <template slot-scope="scope">
          {{ scope.row.companyName | noValue }}
        </template>
      </el-table-column>
      <el-table-column prop="productName" label="数量" width="150">
        <template slot-scope="scope">
          <p><span class="mark total">总</span>{{ scope.row.quantity  | thousandSep }}</p>
          <p><span class="mark surplus">余</span>{{ scope.row.surpQuantity  | thousandSep }}</p>
        </template>
      </el-table-column>

      <el-table-column prop="smoney" label="金额" width="150">
        <template slot-scope="scope">
          <p><span class="mark total">总</span>{{ scope.row.smoney | thousandSep }}</p>
          <p><span class="mark surplus">余</span>{{ scope.row.surpMoney | thousandSep }}</p>
        </template>
      </el-table-column>

      <el-table-column prop="state" align="left" label="状态" min-width="150">
        <template slot-scope="scope">
          <div class="state" v-if=" scope.row.state === -1">
            <span></span>{{ $dictUtils.getDictLabel("busState", scope.row.state, "") }}
          </div>
          <div class="state empty" v-if=" scope.row.state === 1">
            <span></span>{{ $dictUtils.getDictLabel("busState", scope.row.state, "") }}
          </div>
          <div class="state process" v-if=" scope.row.state === 2">
            <span></span>{{ $dictUtils.getDictLabel("busState", scope.row.state, "") }}
          </div>
          <div class="state process" v-if=" scope.row.state === 3 || scope.row.state === 4 " type="danger"><span></span>{{
              $dictUtils.getDictLabel("busState", scope.row.state, "")
            }}
          </div>
          <div class="state normal" v-if=" scope.row.state === 100">
            <span></span>{{ $dictUtils.getDictLabel("busState", scope.row.state, "") }}
          </div>
          <div class="state normal" v-if=" scope.row.state === 101">
            <span></span>{{ $dictUtils.getDictLabel("busState", scope.row.state, "") }}
          </div>
          <div class="state normal" v-if=" scope.row.state === 102">
            <span></span>{{ $dictUtils.getDictLabel("busState", scope.row.state, "") }}
          </div>
          <div class="state normal" v-if=" scope.row.state === 103">
            <span></span>{{ $dictUtils.getDictLabel("busState", scope.row.state, "") }}
          </div>
        </template>
      </el-table-column>

      <span v-for="(item, index) in checkedProps" :key="index">
        <el-table-column prop="createBy.loginName" min-width="110px" :label="item" v-if="item==='创建人'">
              <template slot-scope="scope">
                  <span v-if="scope.row.createBy && scope.row.createBy.loginName">
                       {{ scope.row.createBy.loginName | noValue }}
                  </span>
                  <span v-else>--</span>
              </template>
          </el-table-column>
          <el-table-column prop="createDate" :label="item" min-width="200px" v-if="item==='创建时间'">
            <template slot-scope="scope">
                {{ scope.row.createDate | noValue }}
            </template>
        </el-table-column>
        <el-table-column prop="updateBy.loginName" min-width="120px" :label="item" v-if="item==='上次操作人'">
              <template slot-scope="scope">
                  <span v-if="scope.row.updateBy && scope.row.updateBy.loginName">
                     {{ scope.row.updateBy.loginName | noValue }}
                  </span>
                  <span v-else>--</span>
              </template>
          </el-table-column>
          <el-table-column prop="updateDate" :label="item" min-width="200px" v-if="item==='上次操作时间'">
              <template slot-scope="scope">
                  {{ scope.row.updateDate | noValue }}
              </template>
          </el-table-column>
          <el-table-column prop="dataType" :label="item" v-if="item === '数据类别'" width="130">
        <template slot-scope="scope">
          {{ $dictUtils.getDictLabel("dataType", scope.row.dataType, "") | noValue }}
        </template>
      </el-table-column>
          <el-table-column prop="executeStartDate" :label="item" v-if="item === '指定执行时间'" width="220">
        <template slot-scope="scope">
          {{ scope.row.executeStartDate | noValue }} ~ {{ scope.row.executeEndDate | noValue }}
        </template>
      </el-table-column>
      </span>

      <el-table-column prop="address" label="执行情况" width="112">
        <template slot-scope="scope">
          <el-link type="primary" :underline="false" style="padding-right: 10px" @click="openInfo(scope.row.id)">
            {{ scope.row.weightNumber }}
          </el-link>

        </template>
      </el-table-column>
      <el-table-column prop="address" label="操作" width="200">
        <template slot-scope="scope">
          <el-link type="primary" :underline="false" style="padding-right: 10px"
                   @click="getSettingClient(scope.row,'设置')">设置
          </el-link>
          <el-link type="primary" :underline="false" style="padding-right: 10px"
                   @click="handleCommand(showName(scope.row).command,scope.row)">
            {{ showName(scope.row)?showName(scope.row).name:'--' }}
          </el-link>
          <el-dropdown @command="handleCommand($event,scope.row)" trigger="click">
               <span class="el-dropdown-link">
               更多<i class="el-icon-arrow-down el-icon--right"></i>
               </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item :command="item.command" v-for="item in optionsList" v-if="showOptions(item,scope.row)">
                {{ item.name }}
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>


    <el-pagination
      background
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :page-size="page.pageSize"
      :current-page="page.pageNo"
      layout="total,sizes,prev, pager, next, jumper"
      :total="page.total"
    >
    </el-pagination>
    <!--产品选择弹窗-->
    <!--    <ProductDialog ref="ProductDialog" @getProductData="getProductData"></ProductDialog>-->
    <!--客户选择弹窗-->
    <!--    <CompanyDialog ref="CompanyDialog" @getCompanyData="getCompanyData"></CompanyDialog>-->
    <!--相关设置弹窗-->
    <BusinessDialog ref="BusinessDialog" :businessId="this.businessId" :procInsId="this.procInsId">

    </BusinessDialog>
    <showNumber ref="showNumber" :businessId="this.businessId" :procInsId="this.procInsId"></showNumber>
    <!--  执行情况   -->
    <el-dialog
      width = "1250px"
      title="执行明细"
      :visible.sync="innerVisible"
      class="tableDialog"
      append-to-body
      :close-on-click-modal="false"

    >
      <div class="exception-table-title" style="color: #999">
        已过磅<span> {{ weightNumber?weightNumber:0}} </span>车，合计<span style="color: #1890FF"> {{ weightTon?weightTon:0 }} </span>吨，剩余数量<span style="color: #FF3E3E"> {{ surplusWeightTon?surplusWeightTon:0 }} </span>吨
      </div>


      <el-table
        size="mini"
        v-loading="infoLoading"
        :data="infoListData"
        style="width: 100%; margin-top: 10px"
        border
        :header-cell-style="{
        'background-color': '#eeeeee !important',
      }">


        <el-table-column type="index" label="序号" width="60" align="center">
          <template slot-scope="scope">
          <span>{{
              (infoPage.pageNo - 1) * infoPage.pageSize + scope.$index + 1
            }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="过磅日期"
          prop="weightTime"
          :show-overflow-tooltip="true"
        >
        </el-table-column>
        <el-table-column
          label="过磅单"
          prop="weightCode"
          :show-overflow-tooltip="true"
        >
          <template slot-scope="scope">
            {{scope.row.weightCode| noValue}}
          </template>
        </el-table-column>
        <el-table-column
          label="货物名称"
          prop="productName"
          :show-overflow-tooltip="true"
        >
          <template slot-scope="scope">
            {{scope.row.productName| noValue}}
          </template>
        </el-table-column>
        <el-table-column
          label="车牌号"
          prop="truckNumber"
          :show-overflow-tooltip="true"
        >
          <template slot-scope="scope">
            {{scope.row.truckNumber| noValue}}
          </template>
        </el-table-column>
        <el-table-column
          label="司机"
          prop="driverName"
          :show-overflow-tooltip="true"
        >
          <template slot-scope="scope">
            {{scope.row.driverName| noValue}}
          </template>
        </el-table-column>
        <el-table-column
          label="单价"
          prop="productPrice"
          :show-overflow-tooltip="true"
          align="right"
        >
        </el-table-column>
        <el-table-column
          label="数量"
          prop="productPrice"
          :show-overflow-tooltip="true"
        >
        </el-table-column>
        <el-table-column
          label="金额"
          prop="productPrice"
          :show-overflow-tooltip="true"
          align="right"
        >
        </el-table-column>
        <el-table-column
          label="支付方式"
          prop="payMode"
          :show-overflow-tooltip="true"
        >
          <template slot-scope="scope">
            {{ $dictUtils.getDictLabel("payMode", scope.row.payMode, "") }}
          </template>
        </el-table-column>
        <el-table-column prop="paymentState" label="支付状态" width="130" align="left">
          <template slot-scope="scope">
            <div class="state" v-if=" scope.row.paymentState === -1">
              <span></span>{{ $dictUtils.getDictLabel("busState", scope.row.paymentState, "") }}
            </div>
            <div class="state empty" v-if=" scope.row.paymentState === 1">
              <span></span>{{ $dictUtils.getDictLabel("busState", scope.row.paymentState, "") }}
            </div>
            <div class="state process" v-if=" scope.row.paymentState === 2">
              <span></span>{{ $dictUtils.getDictLabel("busState", scope.row.paymentState, "") }}
            </div>
            <div class="state process" v-if=" scope.row.paymentState === 3 || scope.row.paymentState === 4 "
                 type="danger"><span></span>{{ $dictUtils.getDictLabel("busState", scope.row.paymentState, "") }}
            </div>
            <div class="state normal" v-if=" scope.row.paymentState === 100">
              <span></span>{{ $dictUtils.getDictLabel("busState", scope.row.paymentState, "") }}
            </div>
          </template>
        </el-table-column>
        <el-table-column
          label="支付时间"
          prop="paymentTime"
          :show-overflow-tooltip="true"
        >
        </el-table-column>
      </el-table>
      <el-pagination
        background
        @size-change="infoHandleSizeChange"
        @current-change="infoHandleCurrentChange"
        :page-size="infoPage.pageSize"
        :current-page="infoPage.pageNo"
        layout="total,prev, pager,next"
        :total="infoPage.total"

      >
      </el-pagination>

    </el-dialog>
  </div>
</template>
<script>
import {
  getBusinessList,
  updateBusinessState,
  deleteBusiness,
  getStateById,
  getModelInfo,
  getBusinessInfoList
} from '@/api/business/business.js'
import {getDictModelList, getBusControlVar} from '@/api/model/model.js'
import PopoverBut from '@/components/button/popoverBut'
import ProductDialog from '@/components/dialog/productDialog.vue'
import CompanyDialog from '@/components/dialog/companyDialog.vue'
import BusinessDialog from '@/components/dialog/businessDialog.vue'

const options1 = []
const cityOptions = ['上海', '北京']
export default {
  components: {
    // EditForm,
    ProductDialog,
    // TimeSelect,
    CompanyDialog,
    BusinessDialog,
    PopoverBut,
    companyName: () => import('@/components/autoSelectName/companyName'),
    productName: () => import('@/components/autoSelectName/productName'),
    showNumber: () => import('@/components/dialog/showNumber')
  },
  inject: ['processType'],
  data () {
    return {
      procInsId: '',
      optionsList: [
        {
          command: 'edit',
          permission: 'busin:busin:edit',
          state: '1',
          name: '修改'
        },
        {
          command: 'del',
          permission: 'busin:busin:del',
          state: '1',
          name: '删除'
        },
        {
          command: 'confirm',
          permission: 'busin:busin:edit',
          state: '1',
          name: '提交'
        },
        {
          command: 'clear',
          permission: 'busin:busin:edit',
          state: '101,100',
          name: '中止'
        },
        {
          command: 'finish',
          permission: 'busin:busin:edit',
          state: '100,101',
          name: '完成'
        },
        {
          command: 'stop',
          permission: 'busin:busin:edit',
          state: '100',
          name: '暂停'
        },
        {
          command: 'recovery',
          permission: 'busin:busin:edit',
          state: '101',
          name: '恢复'
        },
        {
          command: 'continue',
          permission: 'busin:busin:edit',
          state: '100,101,102,103',
          name: '续签'
        },
        {
          command: 'withdraw',
          permission: 'busin:busin:edit',
          state: '3',
          name: '撤回'
        },
        {
          command: 'changeNum',
          permission: 'busin:busin:edit',
          state: '99',
          name: '数量调整'
        }
      ], // 操作list 数据
      innerVisible: false,
      infoResData: {},
      times: [],        // 开始时间结束时间
      businessIds: [], // 选中的数据id数组
      businessId: '',
      modelId: '',
      productValue: '产品',
      companyValue: '客户',
      modelIds: [], // 选中的数据modelId数组
      processTypeData: [],
      showMore: false,
      ckecked: '',
      value3: '',
      value4: '',
      queryForm: {
        keyWord: '',
        modelId: '',
        dateTime: [],
        procInsId: '',
        state: '',
        getMoneyType: '',
        dataType: '',
        contractCode: '',
        companyId: '',
        companyName: '',
        productId: '',
        productName: '',
        checkedState: '1',
        name: ''
      },
      value2: '',
      checkedProps: options1,
      ShowCloum: {createDate: ''},
      options: [
        {label: '数据类别', key: 'dataType'},
        {label: '指定执行时间', key: 'executeStartDate'},
        {label: '创建人', key: 'createBy.loginName'},
        {label: '创建时间', key: 'createDate'},
        {label: '上次操作人', key: 'updateBy.loginName'},
        {label: '上次操作时间', key: 'updateDate'}
      ],
      tableData: [],
      infoListData: [],
      weightNumber: 0,
      weightTon: '',
      surplusWeightTon: 0,
      orderBy: {
        name: 'create_date',
        method: 'desc'
      },
      loading: false,
      infoLoading: false,
      page: {
        pageSize: 10,
        pageNo: 1,
        total: 0
      },
      infoPage: {
        pageSize: 10,
        pageNo: 1,
        total: 0
      },
      props: 1,
      checkedCities: [],
      cities: cityOptions
    }
  },
  props: ['typeFather'],
  methods: {
    /*
        *  新增判断 操作更多中展示的数据
        * */
    showOptions (data, row) {
      let show = false
      let key = data.permission
      if (data.state.split(',').indexOf(String(row.state)) > -1 && this.hasPermission(key) && this.showName(row).command !== data.command) {
        show = true
      }
      if (String(data.state) === '99') {
        show = true
      }
      return show
    },
    // 判断动态释放哪个名字 (保证更多永远是有3个选项在外面的)
    showName (data) {
      let options = this.optionsList
      for (let i = 0; i < options.length; i++) {
        let key = options[i].permission
        let state = options[i].state
        if (this.hasPermission(key) && state.split(',').indexOf(String(data.state)) > -1) {
          return options[i]
        }
      }
    },
    getTime1 (e) {
      this.times = e
      this.value3 = e[0]
      this.value4 = e[1]
    },
    saveSearch () {
      localStorage.setItem('queryForm', JSON.stringify(this.queryForm))
      localStorage.setItem('ckecked', JSON.stringify(this.ckecked))
      localStorage.setItem('value3', this.value3)
      localStorage.setItem('value4', this.value4)
      this.$message({
        message: '已为您设置为常用查询条件',
        type: 'success'
      })
    },

    refreshList () {
      this.getList(
        this.processType,
        this.page,
        this.orderBy.name,
        this.orderBy.method,
        this.queryForm.dateTime,
        this.queryForm.state,
        this.queryForm.getMoneyType,
        this.queryForm.companyName,
        this.queryForm.productId,
        this.queryForm.modelId,
        this.queryForm.dataType,
        this.queryForm.keyWord
      )
    },
    // 是否显示某列数据
    handlerCloumn (e) {
      this.checkedProps = e
    },
    // 显示更多筛选条件
    handleShowMore () {
      this.showMore = !this.showMore
      this.queryForm.keyWord = ''
    },
    changeTime (value) {
      this.ckecked = value
      if (value === '1') {
        const start = new Date(new Date(new Date().toLocaleDateString()).getTime())
        const end = new Date(new Date(new Date().toLocaleDateString()).getTime() + 24 * 60 * 60 * 1000 - 1)
        this.queryForm.dateTime = [start, end]
      }
      if (value === '2') {
        const start = getMonday('s', 0)
        const end = getMonday('e', 0)
        const newStart = start + ' 00:00:00'
        const newEnd = end + ' 23:59:59'
        this.queryForm.dateTime = [newStart, newEnd]
      }
      if (value === '3') {
        const start = getMonth('s', 0)
        const end = getMonth('e', 0)
        const newStart = start + ' 00:00:00'
        const newEnd = end + ' 23:59:59'
        this.queryForm.dateTime = [newStart, newEnd]
      }
      if (value === '4') {
        const start = getMonth('s', -1)
        const end = getMonth('e', -1)
        const newStart = start + ' 00:00:00'
        const newEnd = end + ' 23:59:59'
        this.queryForm.dateTime = [newStart, newEnd]
      }
      if (value === '5') {
        const start = getYear('s', 0)
        const end = getYear('e', 0)
        const newStart = start + ' 00:00:00'
        const newEnd = end + ' 23:59:59'
        this.queryForm.dateTime = [newStart, newEnd]
      }
    },
    // 多选
    tableSelected (val) {
      // 去重获取被选中的id值存入到数组中
      this.businessIds = []
      this.modelIds = []
      for (let selectedItem of val) {
        this.businessIds.push(selectedItem.id)
        this.modelIds.push(selectedItem.modelId)
      }
    },
    // 更多操作
    handleCommand ($event, row) {
      console.log($event, row);
      if ($event == 'edit') {
        this.edit(row.id)
      } else if ($event == 'del') {
        this.del(row.id)
      } else if ($event == 'confirm') {
        this.confirm(row.id)
      } else if ($event == 'clear') {
        this.updateState(row.id, 103)
      } else if ($event == 'finish') {
        this.updateState(row.id, 102)
      } else if ($event == 'stop') {
        this.updateState(row.id, 101)
      } else if ($event == 'recovery') {
        this.updateState(row.id, 100)
      } else if ($event == 'continue') {
        this.updateState(row.id, 999)
      } else if ($event == 'withdraw') {
        // this.updateState(row.id,103)
      } else if ($event == 'changeNum') {
        // 显示数量调整-- 流程图-- 弹窗信息
        this.businessId = row.id
        this.modelId = row.modelId
        this.procInsId = row.procInsId || ''
        this.$refs['showNumber'].init('数量调整')
        // this.updateState(row.id,103)
      }
    },
    confirm (id) {
      getModelInfo({
        id: id
      }).then((res) => {
        if (res.data.code === '200') {
          this.$router.push({
            path: '/form/template',
            query: {
              title: res.data.result.modelName,
              id: res.data.result.modelId,
              previewMode: true,
              method: 'confirm',
              businessId: id
            }
          })
        } else {
          this.$message({
            type: 'error',
            message: res.data.message
          })
        }
        this.loading = false
      })
    },
    // 导出
    exportExcel (processType, params, orderName, orderByAORC, queryDateTime, queryState, getMoneyType, companyName, productId, modelId, dataType, keyWord) {
      this.$http({
        method: 'get',
        url: '/business/export',
        params: {
          processType: processType,
          pageSize: params.pageSize,
          pageNo: params.pageNo,
          orderBy: orderName,
          orderByAORC: orderByAORC,
          contractStartDate: this.value3,
          contractEndDate: this.value4,
          state: queryState,
          getMoneyType: getMoneyType,
          companyName: companyName,
          productId: productId,
          modelId: modelId,
          dataType: dataType,
          keyWord: keyWord
        },
        responseType: 'blob'
      }).then(response => {
        if (!response) {
          return
        }
        let link = document.createElement('a')
        link.href = window.URL.createObjectURL(new Blob([response.data]))
        link.target = '_blank'
        let filename = response.headers['content-disposition']
        link.download = decodeURI(filename)
        document.body.appendChild(link)
        link.click()
        document.body.removeChild(link)
        // eslint-disable-next-line handle-callback-err
      }).catch((error) => {

      })
    },
    // 状态修改
    updateState (id, state) {
      var str = ''

      if (state === 101) { // 暂停
        str = '暂停后，该业务将不可用！确定要暂停吗？'
        this.update(str, id, state)
      } else if (state === 100) { // 恢复
        str = '恢复后，该业务可被继续使用！确定要恢复吗？'
        this.update(str, id, state)
      } else if (state === 102) { // 完成
        str = '确认完成该业务吗？'
        this.update(str, id, state)
      } else if (state === 999) { // 完成
        getStateById({
          id: id
        }).then((res) => {
          if (res.data.code === '200') {
            if (res.data.result.state === 100) {
              str = '确认续签该业务吗？'
              this.update(str, id, state)
            } else {
              this.$notify.error({
                title: '错误',
                message: this.$dictUtils.getDictLabel('busState', res.data.result.state, '') + '该状态不支持续签！'
              })
            }
          }
          this.loading = false
        })
      } else if (state === 103) { // 中止
        getBusControlVar({
          businessId: id
        }).then((res) => {
          if (res.data.code === '200') {
            if (res.data.result.svalue === '1') {
              str = '中止操作后，业务余量将不可用，确定要中止吗？'
            } else if (res.data.result.svalue === '2') {
              str = '中止操作后，业务余额将自动结转至活动余额中，同时改业务将不可用，确定要中止吗？'
            } else if (res.data.result.svalue === '3') {
              str = '中止操作后，业务将不可用，确定要中止吗？'
            }
            this.update(str, id, state)
          }
        })
      }
    },

    update (str, businessId, state) {
      this.$confirm(str, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then((res) => {
        updateBusinessState({
          state: state,
          ids: businessId
        }).then((res) => {
          this.loading = true
          if (res.data.code === '200') {
            this.getList(this.processType, this.page, this.orderBy.name, this.orderBy.method)
            this.$message({
              type: 'success',
              message: '状态修改成功'
            })
          } else {
            this.$message({
              type: 'error',
              message: res.data.message
            })
          }
          this.loading = false
        })
      })
    },
    // 删除业务
    del (id) {
      getStateById({
        id: id
      }).then((res) => {
        if (res.data.code === '200') {
          if (res.data.result.state === 1) {
            this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            }).then((res) => {
              deleteBusiness({
                ids: id
              }).then((res) => {
                if (res.data.code === '200') {
                  this.getList(this.processType, this.page, this.orderBy.name, this.orderBy.method)
                  this.$message({
                    type: 'success',
                    message: '删除成功'
                  })
                } else {
                  this.$message({
                    type: 'error',
                    message: res.data.message
                  })
                }
                this.loading = false
              })
            })
          } else {
            this.$notify.error({
              title: '错误',
              message: this.$dictUtils.getDictLabel('busState', res.data.result.state, '') + '该状态不支持删除！'
            })
          }
        }
        this.loading = false
      })
    },
    goToAddForm () {
      var name = ''
      if (this.processType == 1) {
        name = '新增销售业务'
      } else if (this.processType == 2) {
        name = '新增采购业务'
      } else {
        name = '新增其他业务'
      }
      // this.processType
      this.$router.push({
        path: '/business/business/addBusinessView',
        query: {
          title: name, processType: this.processType
        }
      })
    },
    // 客户选择弹窗
    getCompanyClient () {
      this.$refs['CompanyDialog'].dialogVisible = true
    },
    // 客户选择数据回显
    getCompanyData (row) {
      this.queryForm.companyName = row.names
      this.queryForm.companyId = row.id
    },
    // 产品选择弹窗
    getProductClient () {
      this.$refs['ProductDialog'].page.pageNo = 1
      this.$refs['ProductDialog'].getProductList(10, 1)
      this.$refs['ProductDialog'].dialogVisible = true
    },
    getProductData (row) {
      this.queryForm.productName = row.names
      this.queryForm.productId = row.id
    },
    // 执行明细弹窗
    openInfo (businessId) {
      this.innerVisible = true
      this.businessId = businessId
      this.getInfoList(businessId, this.infoPage)
    },
    // 相关设置弹窗
    getSettingClient (row, title) {
      this.businessId = row.id
      this.modelId = row.modelId
      this.procInsId = row.procInsId || ''
      this.$refs['BusinessDialog'].init(title)
    },
    // 相关设置弹窗
    edit (id) {
      getStateById({
        id: id
      }).then((res) => {
        if (res.data.code === '200') {
          // var state = s.getDictLabel("busState",res.data.result.state, "")
          if (res.data.result.state === 1) {
            this.$router.push({
              path: '/form/template',
              query: {
                title: res.data.result.modelName,
                id: res.data.result.modelId,
                previewMode: true,
                method: 'edit',
                businessId: id
              }
            })
          } else {
            this.$notify.error({
              title: '错误',
              message: this.$dictUtils.getDictLabel('busState', res.data.result.state, '') + '该状态不支持修改！'
            })
          }
        }
        this.loading = false
      })
    },
    // 获取列表带分页  this.getList(, , , )
    /*
    *  allState:3,  //执行中
    * allState:2,   //未生效
    * allState:'',  // 全部
    * */
    getList () {
      this.loading = true
      getBusinessList({
        processType: this.processType,
        pageSize: this.page.pageSize,
        pageNo: this.page.pageNo,
        orderBy: this.orderBy.name,
        orderByAORC: this.orderBy.method,
        contractStartDate: this.value3,
        contractEndDate: this.value4,
        state: this.queryForm.state,
        allState: this.$parent.selectValue === '1' ? '' : this.$parent.selectValue === '2' ? 2 : 3,
        getMoneyType: this.queryForm.getMoneyType,
        companyName: this.queryForm.companyName,
        productId: this.queryForm.productId,
        modelId: this.queryForm.modelId,
        dataType: this.queryForm.dataType,
        keyWord: this.queryForm.keyWord
      }).then((res) => {
        if (res.data.code === '200') {
          this.tableData = res.data.result.list
          this.page.total = res.data.result.count
        }
        this.loading = false
      })
    },

    // 获取列表带分页
    getInfoList (businessId, params) {
      this.infoLoading = true
      getBusinessInfoList({
        businessId: businessId,
        pageSize: params.pageSize,
        pageNo: params.pageNo
      }).then((res) => {
        if (res.data.code === '200') {
          this.weightNumber = res.data.result.weightNumber
          this.weightTon = res.data.result.weightTon
          this.surplusWeightTon = res.data.result.surplusWeightTon
          this.infoListData = res.data.result.page.list
          this.infoPage.total = res.data.result.page.count
        }
        this.infoLoading = false
      })
    },
    // 获取业务类型字典
    getDictModelList (processType) {
      this.loading = true
      getDictModelList({
        processType: processType
      }).then((res) => {
        if (res.data.code === '200') {
          this.processTypeData = res.data.result
        }
        this.loading = false
      })
    },
    // 初始化分页
    resetPagination () {
      this.showMore = false
      Object.assign(this.$data.page, this.$options.data().page)
    },
    // 重置查询条件
    reset () {
      this.queryForm.modelId = ''
      this.queryForm.dateTime = ''
      this.queryForm.state = ''
      this.queryForm.getMoneyType = ''
      this.queryForm.dataType = ''
      this.queryForm.contractCode = ''
      this.queryForm.companyId = ''
      this.queryForm.companyName = ''
      this.queryForm.productId = ''
      this.queryForm.productName = ''
      this.queryForm.checkedState = ''
      this.queryForm.name = ''
      this.value3 = ''
      this.value4 = ''
      this.ckecked = ''
      this.getList()
    },
    // 排序
    sortthiscolumn (a) {
      this.orderBy.name = a.prop
      this.page.pageNo = 1
      if (a.order === 'ascending') {
        this.orderBy.method = 'asc'
      } else {
        this.orderBy.method = 'desc'
      }
      this.getList(this.processType, this.page, this.orderBy.name, this.orderBy.method)
    },
    handleSizeChange (e) {
      this.page.pageSize = e
      this.getList()
    },
    handleCurrentChange (e) {
      this.page.pageNo = e
      this.getList(this.processType, this.page, this.orderBy.name, this.orderBy.method)
    },
    infoHandleSizeChange (e) {
      this.infoPage.pageSize = e
      this.getInfoList(this.businessId, this.infoPage)
    },
    infoHandleCurrentChange (e) {
      this.infoPage.pageNo = e
      this.getInfoList(this.businessId, this.infoPage)
    },
    tableRowClassName ({row, rowIndex}) {
      if (rowIndex % 2 === 1) {
        return 'warning-row'
      } else {
        return 'success-row'
      }
    }
  },
  mounted () {
    if (this.processType == 1) {
      this.productValue = '产品'
      this.companyValue = '客户'
    } else if (this.processType == 2) {
      this.productValue = '原料'
      this.companyValue = '供应商'
    }
    // this.tableData = [{ names: '1' }]
    // if ((localStorage.getItem('queryForm') != null)) {
    //   this.queryForm = JSON.parse(localStorage.getItem('queryForm'))
    //   this.ckecked = JSON.parse(localStorage.getItem('ckecked'))
    //   this.value3 = localStorage.getItem('value3')
    //   this.value4 = localStorage.getItem('value4')
    // }

    // this.getList(this.processType, this.page, this.orderBy.name, this.orderBy.method)
  }
}
</script>
<style scoped lang="scss">
::v-deep.el-textarea__inner::-webkit-input-placeholder {
  color: #999999;
}
@import './index.scss';
  .tableDialog{
    ::v-deep.el-pagination{
      text-align: right;
      margin-top:30px;
    }
}
</style>
